// pages/my/my-loanRecord/my-loanRecord.js
var request = require('../../../utils/request.js');
var util = require('../../../utils/util.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    curId:1,
    // 页面中的数据
    list:[],
    showTip:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.curId){
      that.setData({
        curId: options.curId
      })
    }
  
   
  },
  onShow: function () {
    var that = this;
    that.getToken();
  },



  getToken: function () {
    console.log(getCurrentPages())
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        var tempToken = res.data;
        console.log({
          "session_id": tempToken,
          "is_pay": that.data.curId
        })
        request.getOrderList(
          {
            "session_id": tempToken,
            "is_pay": that.data.curId
          },
          (res) => {
            console.log(res);
            if (res.data.status == "success") {
              if (res.data.code == "1") {
                that.setData({
                  list: res.data.data,
                  showTip: false
                })
                console.log(res.data.data.length);
                for (var i = 0; i < res.data.data.length; i++) {
                  if (that.data.list[i].l_adress){
                  that.data.list[i].l_adress = util.cutTitleString(res.data.data[i].l_adress, 0, 12)
                  }
                  if (that.data.list[i].name){

                  that.data.list[i].name = util.cutTitleString(res.data.data[i].name, 0, 12)
                  }
                  if (that.data.list[i].order_no){
                  that.data.list[i].order_no = util.cutTitleString(res.data.data[i].order_no, 0, 15)

                  }
                }
                that.setData({
                  list: that.data.list
                })
               
              }
              else if (res.data.code == "10002") {
                that.setData({
                  showTip: true
                })
                

              }
            } else {
              wx.showToast({
                title: '请求失败',
                image: "../../../images/xx.png"
              })
            }
            wx.hideLoading();
          }
        )



      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },
 
  //切换导航栏菜单
  tabChoose:function(e){
    var that = this;
    that.setData({
      curId: e.target.dataset.id,
    })
    that.getToken();
  },
  //跳转到详情页
  goOrderDesc:function(e){
    console.log(getCurrentPages())
    var that = this;
    wx.navigateTo({
      url: '../my-orderDesc/my-orderDesc?id=' + e.currentTarget.dataset.id + "&curId=" + that.data.curId,
    })
  },
  goIndex:function(){
    wx.navigateBack({
      delta:4
    })
  }
})